package com.songcha.library_business.proxy.database

import com.songcha.library_business.helper.DaoManagerHelper
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.util.LogUtil
import com.songcha.library_database_douyue.bean.BookShelfDBBean
import com.songcha.library_database_douyue.greendao.BookDetailDBBeanDao
import com.songcha.library_database_douyue.greendao.BookShelfDBBeanDao
import com.songcha.library_database_douyue.manager.GreenDaoManager
import org.greenrobot.greendao.query.WhereCondition.StringCondition

class BookShelfDaoManager {
    companion object{

        @JvmStatic
        fun addToBookShelf(bookId: String):Boolean{
            if(bookId.isBlank()) return false
            try{
                val dao=GreenDaoManager.getInstance().bookShelfDBBeanDao
                val bean=BookShelfDBBean().apply {
                    this.bookId=bookId
                    userId=UserInfoManager.userId
                }
                val qb=dao.queryBuilder()
                qb.where(BookShelfDBBeanDao.Properties.BookId.eq(bookId))
                    .where(BookShelfDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                if(qb.list().size==0){
                    dao.insert(bean)
                }
                return true
            }catch (e:Exception){
                e.printStackTrace()
                return false
            }

        }

        @JvmStatic
        fun removeFromBookShelf(bookId: String){
            if(bookId.isBlank()) return
            val dao=GreenDaoManager.getInstance().bookShelfDBBeanDao
            val bean=BookShelfDBBean().apply {
                this.bookId=bookId
                userId=UserInfoManager.userId
            }
            val qb=dao.queryBuilder()
            qb.where(BookShelfDBBeanDao.Properties.BookId.eq(bookId))
                .where(BookShelfDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
            qb.buildDelete().executeDeleteWithoutDetachingEntities()
        }

        @JvmStatic
        fun isInBookShelf(bookId: String):Boolean{
            if(bookId.isBlank()) return false
            val dao=GreenDaoManager.getInstance().bookShelfDBBeanDao
            val qb=dao.queryBuilder()
            qb.where(BookShelfDBBeanDao.Properties.BookId.eq(bookId))
                .where(BookShelfDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
            return (qb.list().size>0)
        }

        @JvmStatic
        fun getBookShelfList():MutableList<BookShelfDBBean>{
            try{
                val dao=GreenDaoManager.getInstance().bookShelfDBBeanDao
                val qb=dao.queryBuilder()
                qb.where(BookShelfDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                return qb.list()
            }catch (e:Exception){
                e.printStackTrace()
                return mutableListOf()
            }
        }


        @JvmStatic
        fun delBookShelfByIds(ids:MutableList<String>){
            if(ids.size==0) return
            try{
                val dao=GreenDaoManager.getInstance().bookShelfDBBeanDao
                val qb=dao.queryBuilder()
                qb.where(BookShelfDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                    .where(StringCondition(BookShelfDBBeanDao.Properties.BookId.columnName+ " in "+DaoManagerHelper.getRangeStringByIds(ids)))
                qb.buildDelete().executeDeleteWithoutDetachingEntities()
            }catch (e:Exception){
                e.printStackTrace()
            }
        }


    }
}